<template>
  <div>
    <el-card>
      <template #header>
        <el-form :model="form" inline>
          <el-form-item label="数据编码:">
            <el-input
              clearable
              v-model="form.dataCode"
              placeholder="请输入配置项编码"
              @input="enterChange"
            >
              <template #prefix>
                <el-icon class="el-input__icon"><search /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="企业名称">
            <el-select
              v-model="form.enterpriseCode"
              filterable
              clearable
              placeholder="请选择企业"
              @change="enterChange"
              style="width: 200px"
            >
              <el-option
                v-for="item in options"
                :key="item.enterpriseId"
                :label="item.cmpName"
                :value="item.enterpriseCode"
              />
              <template #prefix>
                <el-icon class="el-input__icon"><search /></el-icon>
              </template>
            </el-select>
          </el-form-item>
          <div style="float: right">
            <el-button @click="add" type="primary"
              >新增<el-icon style="margin-left: 5px"><Plus /></el-icon>
            </el-button>
          </div>
        </el-form>
      </template>
      <el-table
        ref="enterpriseRef"
        :data="tableData"
        style="width: 100%; max-height: 61vh; height: 61vh"
        table-layout="fixed"
        v-loading="loading"
        :header-cell-style="{'text-align':'center'}"
        :cell-style="{'text-align':'center'}"
      >
        <el-table-column
          fixed
          min-width="70"
          prop="collectitemName"
          label="配置项名称"
        />
        <el-table-column prop="enterpriseCode" label="统一社会信用代码" />
        <el-table-column prop="dataCode" label="采集数据项编码" />
        <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="upload(scope.$index, scope.row)"
              >上报</el-button
            >
            <el-button
                :disabled="scope.row.statType !== '1'"
                link
                type="primary"
                @click="priseData(scope.$index, scope.row)"
            >数据映射</el-button
            >
            <el-button
              link
              type="primary"
              @click="edit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-popover
              placement="top-start"
              title="删除操作"
              :width="200"
              trigger="click"
              :visible="visibleSS[scope.$index]"
            >
              <p>确定要进行删除吗?</p>
              <div style="text-align: right; margin: 0">
                <el-button
                  size="small"
                  text
                  @click="visibleSS[scope.$index] = false"
                  >取消</el-button
                >
                <el-button
                  size="small"
                  type="primary"
                  @click="del(scope.$index, scope.row)"
                  >确定</el-button
                >
              </div>
              <template #reference>
                <el-button
                  link
                  type="danger"
                  ref="buttonRefs"
                  @click="visibleSS[scope.$index] = true"
                  >删除</el-button
                >
              </template>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="card-footer">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10,20,30,40,50,100,500]"
          background
          v-model:page-size="pagination.size"
          v-model:current-page="pagination.page"
          :total="pagination.total"
          @size-change="sizeChange"
          @current-change="pageChange"
          class="pagination"
        />
      </div>
    </el-card>
    <dataDialog></dataDialog>
    <priseDataDialog></priseDataDialog>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import {
  datacollectconfigure,
  getNameList,
  datacollectconfigureAdd,
  datacollectconfigureDel,
  datacollectconfigureUpload,
} from "@/api/enterprise";
import mitts from "@/utils/bus.js";
import dataDialog from "./dialog/index.vue";
import priseDataDialog from "./dialog/dialog_1.vue"
import { ElMessage } from "element-plus";

const loading = ref(false);
const tableData = ref([]);
const condition = ref([]);
const visibleSS = ref([]);
const options = ref([]);
const pagination = reactive({
  page: 1,
  size: 10,
  total: 0,
});
const form = reactive({
  dataCode: "",
  enterpriseCode: "",
});
const doDataList = () => {
  loading.value = true;
  let datas = {
    pageNum: pagination.page,
    pageSize: pagination.size,
    condition: JSON.stringify(condition.value),
  };
  datacollectconfigure(datas)
    .then((res) => {
      console.log(res);
      tableData.value = res.data.records;
      loading.value = false;
      tableData.value = res.data.records;
      pagination.size = res.data.pageSize;
      pagination.page = res.data.currentPage;
      pagination.total = res.data.total;
    })
    .catch((msg) => {});
  getNameList()
    .then((res) => {
      options.value = res.data;
    })
    .catch((msg) => {});
};
const sizeChange = (value) => {
  pagination.size = value;
  pagination.page = 1;
  doDataList();
};
/**
 * 页总数改变
 */
const pageChange = (value) => {
  pagination.page = value;
  doDataList();
};
const enterChange = () => {
  let cond = [];
  let temp = {};
  if (form.dataCode != "" && form.dataCode != undefined) {
    temp = {
      column: "dataCode",
      type: "like",
      value: form.dataCode,
    };
    cond.push(temp);
  }
  if (form.enterpriseCode != "" && form.enterpriseCode != undefined) {
    temp = {
      column: "enterpriseCode",
      type: "eq",
      value: form.enterpriseCode,
    };
    cond.push(temp);
  }
  condition.value = cond;
  doDataList();
};
const del = (index, rowData) => {
  datacollectconfigureDel(rowData)
    .then((res) => {
     if(res.status == 200){
      ElMessage.success("删除成功！");
      visibleSS.value[index] = false;
      doDataList();
     }else{
      ElMessage.warning("服务器出错,请稍后再试！");
     }
    })
    .catch((msg) => {});
};
const upload = (index, rowData) => {
  datacollectconfigureUpload(rowData)
    .then((res) => {
     if(res.status == 200){
      ElMessage.success("上报成功！");
      doDataList();
     }else{
      ElMessage.warning("服务器出错,请稍后再试！错误信息为："+res.message);
     }
    })
    .catch((msg) => {});
};
const add = () => {
  mitts.emit("visible", true);
  mitts.emit("add", true);
};
const edit = (index, rowData) => {
  mitts.emit("enterprise", rowData);
  mitts.emit("visible", true);
};
const priseData = (index,rowData) =>{
  mitts.emit("enterprise_1",rowData)
  mitts.emit("visible_1",true)
}
mitts.on("isUpload", (e) => {
  doDataList;
});
doDataList();
</script>

<style lang="less" scoped>
.title {
  min-width: 5em;
}
.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.el-card__header {
  max-height: 10vh;
}
.pagination {
  max-height: 3vh;
}
.card-footer {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
}
.total-font {
  margin-right: 15px;
  font-weight: 500;
  line-height: 21px;
  color: #595959;
}
</style>
